package com.kyle.java.LinkedList;


import java.util.Stack;

/**
 * @author kyle
 * @version 1.0
 * @date 2021/4/2 13:42
 */
public class HeroNode {
    private  int id;
    String name;
    String nickName;
    HeroNode next;

    public HeroNode(int id, String name, String nickName) {
        this.id = id;
        this.name = name;
        this.nickName = nickName;
        this.next = next;
    }

    public static void main(String[] args) {
        HeroNode heroNode1 = new HeroNode(1,"a","aaa");
        HeroNode heroNode2 = new HeroNode(2,"b","aaa");
        HeroNode heroNode3 = new HeroNode(3,"c","aaa");
        HeroNode heroNode4 = new HeroNode(4,"c","aaa");
        HeroNode heroNode5 = new HeroNode(5,"c","aaa");
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.add(heroNode1);
        singleLinkedList.add(heroNode2);
        singleLinkedList.add(heroNode3);
        singleLinkedList.add(heroNode4);
        singleLinkedList.add(heroNode5);
        singleLinkedList.listFromBottom();
/*
        System.out.println("节点个数"+singleLinkedList.getNumberOfElem());
        System.out.println("反转链表：");
        singleLinkedList.reverseTheLinkedList();
        singleLinkedList.list();
*/



    }
    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                ", next=" +  (next!=null?next.name:"空") +
                '}';
    }

}
class SingleLinkedList{
    private HeroNode headNode = new HeroNode(0,"","");


    //添加节点
    public void add(HeroNode heroNode){
        HeroNode lastNode = headNode; //尾部添加数据，初始化尾部节点
        while(lastNode.next!=null){ // 使尾部节点指向尾部
            lastNode = lastNode.next;
        }
        lastNode.next = heroNode;
    }

    //打印各个节点的信息
    public void list(){
        if(headNode.next == null){
            System.out.println("list is empty");
        }
        else{
            HeroNode elem = headNode.next;
            while (elem !=null){
                System.out.println(elem.toString());
                elem = elem.next;
            }
        }

    }

    //输出非空节点的个数（不统计头节点)
    public int getNumberOfElem(){
        int count = 0;
        HeroNode temp = headNode;
        while(temp.next!=null){
            count++;
            temp = temp.next;
        }
        return count;
    }
    //查找单链表中的倒数第k个节点(未完成)

    public HeroNode getNodeByBottomNum(int k){
         return null;
    }

    //单链表反转
    public void reverseTheLinkedList(){
       if(headNode.next == null){
           System.out.println("空链表 无需逆转");
       }else{
           HeroNode pNextNodeSave;
           HeroNode pNode = headNode.next; //p节点指向第一个节点
           pNextNodeSave = pNode.next;
           HeroNode pNextNode;
           while(pNextNodeSave!=null){ //如果p节点一直有下一个节点，则做反转
                pNextNode = pNextNodeSave; //得到p的下一个节点
                pNextNodeSave= pNextNode.next;//记录p的下一个节点的下一个节点
                pNextNode.next = pNode;
                pNode = pNextNode;
           }

           headNode.next.next = null;
           headNode.next = pNode;
       }
    }
    //从尾部进行输出打印
    public void listFromBottom(){
        Stack stack = new Stack<HeroNode>();
        HeroNode p = headNode;

        while (p.next!=null){
               p = p.next;
               stack.push(p);
        }

        while(stack.size()>0) {

            System.out.println(stack.pop());
        }


    }
}